//
//  VoiceReadListItemCell.m
//  ShowYork
//
//  Created by hxh on 2021/4/19.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "VoiceReadListItemCell.h"

@interface VoiceReadListItemCell ()
@property (nonatomic,strong)UIImageView *coverImageView;
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UIView *listenCoverView;
@property (nonatomic,strong)UIImageView *listenIcon;
@property (nonatomic,strong)UILabel *listenCountLb;
@property (nonatomic,strong)UIImageView *loadingIcon;
@property (nonatomic,strong)UIButton *coverBtn;
@end

@implementation VoiceReadListItemCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    
    self.backgroundColor = [UIColor clearColor];
    
    CGFloat width = (kScreenW - 17 - 14 - 2 * 16)/3;
   
    CGFloat height = width/3 * 4;
    
    self.coverImageView = [[UIImageView alloc]init];
    self.coverImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.coverImageView.layer.cornerRadius = 8;
    self.coverImageView.layer.masksToBounds = YES;
    self.coverImageView.backgroundColor = RGBA(240, 240, 240, 1);
    [self.contentView addSubview:self.coverImageView];
    [self.coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.contentView);
        make.height.mas_equalTo(height);
    }];
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.text = @"史蒂夫·乔布斯传·第一集";
    self.titleLb.textColor = C51;
    self.titleLb.font = font(12);
    self.titleLb.numberOfLines = 2;
    [self.contentView addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.top.equalTo(self.coverImageView.mas_bottom).offset(8);
    }];
    
    self.listenCoverView = [[UIView alloc]init];
    self.listenCoverView.backgroundColor = RGBA(0, 0, 0, 0.1);
    [self.contentView addSubview:self.listenCoverView];
    [self.listenCoverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.equalTo(self.coverImageView);
        make.size.mas_equalTo(CGSizeMake(60, 18));
    }];
    
    self.listenIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"voice_listen_tag"]];
    [self.contentView addSubview:self.listenIcon];
    [self.listenCoverView addSubview:self.listenIcon];
    [self.listenIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.listenCoverView);
        make.left.equalTo(self.listenCoverView).offset(8);
    }];
    self.listenCountLb = [[UILabel alloc]init];
    self.listenCountLb.text = @"3846";
    self.listenCountLb.textColor = [UIColor whiteColor];
    self.listenCountLb.font = font(10);
    [self.listenCoverView addSubview:self.listenCountLb];
    [self.listenCountLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.listenIcon);
        make.left.equalTo(self.listenIcon.mas_right).offset(4);
    }];
    
    self.loadingIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"voice_loading_1_tag"]];
    [self.contentView addSubview:self.loadingIcon];
    [self.loadingIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLb);
        make.left.equalTo(self.coverImageView);
    }];
    
   
    NSMutableArray *imageArr = [NSMutableArray array];
    for (int i = 0; i < 20; i++) {
        [imageArr addObject:[UIImage imageNamed:[NSString stringWithFormat:@"voice_loading_%i_tag",i+1]]];
    }
    self.loadingIcon.animationImages = imageArr;
    self.loadingIcon.animationDuration = 2.5;
     //动画的重复次数，想让它无限循环就赋成0
    self.loadingIcon.animationRepeatCount = 0;
    
    
    self.coverBtn = [[UIButton alloc]init];
    [self.coverBtn addTarget:self action:@selector(coverMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.coverBtn];
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.contentView);
    }];
    
}
- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.listenCoverView.bounds byRoundingCorners: UIRectCornerTopRight cornerRadii:CGSizeMake(8, 8)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.listenCoverView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.listenCoverView.layer.mask = maskLayer;
}
- (void)setModel:(VoiceReadListItemModel *)model{
    _model = model;
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverLog]];
    NSString *title = @"";
    NSInteger count = model.listenNum.intValue;
    if (model.listenNum.intValue >= 10000) {
        if (count % 1000 == 0) {
            count += 1;
        }
        title = [NSString stringWithFormat:@"%@w",[RPAppManager notRounding:count/10000.0f afterPoint:1]];
    }else{
        title = [NSString stringWithFormat:@"%ld",count];
    }
    self.listenCountLb.text = title;
    
    
    NSMutableParagraphStyle*style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    //对齐方式
    style.alignment = NSTextAlignmentLeft;
    style.headIndent = 0.0f;//行首缩进进
    style.tailIndent = 0.0f;//行尾缩进
    style.lineBreakMode = NSLineBreakByTruncatingTail;
    //首行缩进
    if (model.status == XHItemStatusNormal) {
        [self.loadingIcon stopAnimating];
        self.loadingIcon.hidden = YES;
        style.firstLineHeadIndent = 0;
        self.titleLb.textColor = C51;
    }
    if (model.status == XHItemStatusPlay) {
        self.loadingIcon.hidden = NO;
        [self.loadingIcon startAnimating];
        style.firstLineHeadIndent = 15;
        self.titleLb.textColor = RGBA(238, 157, 59, 1);
    }
    if (model.status == XHItemStatusPause || model.status == XHItemStatusFinish) {
        self.loadingIcon.hidden = NO;
        [self.loadingIcon stopAnimating];
        style.firstLineHeadIndent = 15;
        self.titleLb.textColor = C51;
    }
    NSAttributedString*attrText = [[NSAttributedString alloc] initWithString:model.title attributes:@{NSParagraphStyleAttributeName: style}];
    self.titleLb.attributedText = attrText;
}

- (void)coverMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showVoiceDetailWithCurrentModel:)] ) {
        [self.delegate showVoiceDetailWithCurrentModel:self.model];
    }
}
@end
